﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Lswweb.IDAL;
using Lswweb.Model;

namespace Lswweb.SqlServerDAL
{
    public class SinglePagesDAL : ISinglePages
    {
        private Builder<SinglePageInfo> singlePageBuilder = new Builder<SinglePageInfo>("SinglePages", "Id");

        public SinglePagesDAL()
        {
            singlePageBuilder.CreateModel += new ReaderBind<SinglePageInfo>(singlePageBuilder_CreateModel);
            singlePageBuilder.GetParameters += new ParametersBuilder<SinglePageInfo>(singlePageBuilder_GetParameters);
        }

        IDataParameter[] singlePageBuilder_GetParameters(SinglePageInfo t)
        {
            IDataParameter[] paramters=new IDataParameter[5];

            paramters[0] = DataAccess.CreateParameter("Id",(int)t.Id);

            if (string.IsNullOrEmpty(t.Title))
                paramters[1] = DataAccess.CreateParameter("Title", DBNull.Value);
            else
                paramters[1] = DataAccess.CreateParameter("Title", t.Title);

            if (string.IsNullOrEmpty(t.View))
                paramters[2] = DataAccess.CreateParameter("View", DBNull.Value);
            else
                paramters[2] = DataAccess.CreateParameter("View",t.View);

            paramters[3] = DataAccess.CreateParameter("Ver",t.Ver);

            if (string.IsNullOrEmpty(t.ImageUrl))
                paramters[4] = DataAccess.CreateParameter("@ImageUrl", DBNull.Value);
            else
                paramters[4] = DataAccess.CreateParameter("@ImageUrl", t.ImageUrl);
            

            return paramters;
        }

        SinglePageInfo singlePageBuilder_CreateModel(System.Collections.Hashtable reader)
        {
            SinglePageInfo singlePage = new SinglePageInfo();
            singlePage.Id = (SinglePage)Convert.ToInt32(reader["Id"]);
            if (reader.ContainsKey("Title"))
                singlePage.Title = reader["Title"].ToString();
            if (reader.ContainsKey("View"))
                singlePage.View = reader["View"].ToString();
            if (reader.ContainsKey("Ver"))
                singlePage.Ver = reader["Ver"].ToString();
            if (reader.ContainsKey("ImageUrl"))
                singlePage.ImageUrl = reader["ImageUrl"].ToString();
            
            return singlePage;
        }

        #region ISinglePages 成员

        public SinglePageInfo GetSinglePage(SinglePage singlePage,string ver)
        {
            if (Enum.IsDefined(typeof(SinglePage), singlePage) && !string.IsNullOrEmpty(ver))
                return singlePageBuilder.GetModel("SinglePages", string.Empty, "Id=@Id and Ver=@Ver", "Id asc", DataAccess.CreateParameter("Id", (int)singlePage),DataAccess.CreateParameter("Ver",ver));
            
            return null;
        }

        public bool Update(SinglePageInfo info)
        {
            if (singlePageBuilder.IsHas("Id=@Id and Ver=@Ver",DataAccess.CreateParameter("Id",info.Id),DataAccess.CreateParameter("Ver",info.Ver)))
            {
                string sql = "update SinglePages set Title=@Title,[View]=@View,ImageUrl=@ImageUrl where Id=@Id and Ver=@Ver";
                return singlePageBuilder.Update(sql, info);
            }
            else
            {
                return Insert(info);
            }
        }

        #endregion

        private bool Insert(SinglePageInfo info)
        {
            string sql = "insert into SinglePages(Id,Title,[View],Ver,ImageUrl) values(@Id,@Title,@View,@Ver,@ImageUrl);select Id from SinglePages where Id=@Id;";
            return singlePageBuilder.Insert(sql,info)==(int)info.Id;
        }
    }
}
